package com.chixing.fourteen.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.chixing.fourteen.pojo.Customer;
import com.chixing.fourteen.service.CustomerService;
import com.chixing.fourteen.service.ModelService;
import com.chixing.fourteen.service.impl.CustomerServiceImpl;
import com.chixing.fourteen.service.impl.ModelServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Random;

import static com.alipay.api.AlipayConstants.*;

/**
 * @author Y2014188432
 */
@Controller
@RequestMapping("/user")
public class CustomerController {

    @Autowired
    private CustomerService customerService = new CustomerServiceImpl();

    @Autowired
    private ModelService modelService = new ModelServiceImpl();

    @PostMapping("/login")
    public String login(HttpServletRequest request, String password, String username, Model model){
        Customer customer = customerService.login(username,password);
        HttpSession session = request.getSession();
        if(customer != null){
            session.setAttribute("customer",customer);
            model.addAttribute("customer",customer);
            return "homepage";
        }else{
            return "index";
        }
    }

    @GetMapping("/logout")
    public String logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.invalidate();
        return "index";
    }

    @GetMapping("/getCar")
    public String getCar(){
        List<com.chixing.fourteen.pojo.Model> models = modelService.findAll();
        String jsonObject = JSONObject.toJSONString(models);
        System.out.println(jsonObject);
        return "getCar";
    }

    @GetMapping("/getCarModel")
    @ResponseBody
    public String getCarModel(){
        return JSONObject.toJSONString(modelService.findAll());
    }







}
